{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 单因子数据探索-对每列值进行分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.read_csv('./data/HR.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>satisfaction_level</th>\n",
       "      <th>last_evaluation</th>\n",
       "      <th>number_project</th>\n",
       "      <th>average_monthly_hours</th>\n",
       "      <th>time_spend_company</th>\n",
       "      <th>Work_accident</th>\n",
       "      <th>left</th>\n",
       "      <th>promotion_last_5years</th>\n",
       "      <th>department</th>\n",
       "      <th>salary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>0.38</td>\n",
       "      <td>0.53</td>\n",
       "      <td>2</td>\n",
       "      <td>157</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>0.80</td>\n",
       "      <td>0.86</td>\n",
       "      <td>5</td>\n",
       "      <td>262</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.88</td>\n",
       "      <td>7</td>\n",
       "      <td>272</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0.72</td>\n",
       "      <td>0.87</td>\n",
       "      <td>5</td>\n",
       "      <td>223</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>159</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14997</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.96</td>\n",
       "      <td>6</td>\n",
       "      <td>280</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14998</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>158</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14999</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>158</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>999999.00</td>\n",
       "      <td>2</td>\n",
       "      <td>158</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sale</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15001</td>\n",
       "      <td>0.70</td>\n",
       "      <td>0.40</td>\n",
       "      <td>2</td>\n",
       "      <td>158</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sale</td>\n",
       "      <td>nme</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>15002 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       satisfaction_level  last_evaluation  number_project  \\\n",
       "0                    0.38             0.53               2   \n",
       "1                    0.80             0.86               5   \n",
       "2                    0.11             0.88               7   \n",
       "3                    0.72             0.87               5   \n",
       "4                    0.37             0.52               2   \n",
       "...                   ...              ...             ...   \n",
       "14997                0.11             0.96               6   \n",
       "14998                0.37             0.52               2   \n",
       "14999                 NaN             0.52               2   \n",
       "15000                 NaN        999999.00               2   \n",
       "15001                0.70             0.40               2   \n",
       "\n",
       "       average_monthly_hours  time_spend_company  Work_accident  left  \\\n",
       "0                        157                   3              0     1   \n",
       "1                        262                   6              0     1   \n",
       "2                        272                   4              0     1   \n",
       "3                        223                   5              0     1   \n",
       "4                        159                   3              0     1   \n",
       "...                      ...                 ...            ...   ...   \n",
       "14997                    280                   4              0     1   \n",
       "14998                    158                   3              0     1   \n",
       "14999                    158                   3              0     1   \n",
       "15000                    158                   3              0     1   \n",
       "15001                    158                   2              0     1   \n",
       "\n",
       "       promotion_last_5years department  salary  \n",
       "0                          0      sales     low  \n",
       "1                          0      sales  medium  \n",
       "2                          0      sales  medium  \n",
       "3                          0      sales     low  \n",
       "4                          0      sales     low  \n",
       "...                      ...        ...     ...  \n",
       "14997                      0    support     low  \n",
       "14998                      0    support     low  \n",
       "14999                      0    support     low  \n",
       "15000                      0       sale     low  \n",
       "15001                      0       sale     nme  \n",
       "\n",
       "[15002 rows x 10 columns]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第一列：满意度satisfaction_level分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        0.38\n",
       "1        0.80\n",
       "2        0.11\n",
       "3        0.72\n",
       "4        0.37\n",
       "         ... \n",
       "14997    0.11\n",
       "14998    0.37\n",
       "14999     NaN\n",
       "15000     NaN\n",
       "15001    0.70\n",
       "Name: satisfaction_level, Length: 15002, dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sl_s = df['satisfaction_level']\n",
    "sl_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        False\n",
       "1        False\n",
       "2        False\n",
       "3        False\n",
       "4        False\n",
       "         ...  \n",
       "14997    False\n",
       "14998    False\n",
       "14999     True\n",
       "15000     True\n",
       "15001    False\n",
       "Name: satisfaction_level, Length: 15002, dtype: bool"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 异常值分析\n",
    "sl_s.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14999   NaN\n",
       "15000   NaN\n",
       "Name: satisfaction_level, dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 异常值个数\n",
    "sl_s[sl_s.isnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>satisfaction_level</th>\n",
       "      <th>last_evaluation</th>\n",
       "      <th>number_project</th>\n",
       "      <th>average_monthly_hours</th>\n",
       "      <th>time_spend_company</th>\n",
       "      <th>Work_accident</th>\n",
       "      <th>left</th>\n",
       "      <th>promotion_last_5years</th>\n",
       "      <th>department</th>\n",
       "      <th>salary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>14999</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>158</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>999999.00</td>\n",
       "      <td>2</td>\n",
       "      <td>158</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sale</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       satisfaction_level  last_evaluation  number_project  \\\n",
       "14999                 NaN             0.52               2   \n",
       "15000                 NaN        999999.00               2   \n",
       "\n",
       "       average_monthly_hours  time_spend_company  Work_accident  left  \\\n",
       "14999                    158                   3              0     1   \n",
       "15000                    158                   3              0     1   \n",
       "\n",
       "       promotion_last_5years department salary  \n",
       "14999                      0    support    low  \n",
       "15000                      0       sale    low  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看异常值数据\n",
    "df[sl_s.isnull()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "丢掉空值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        0.38\n",
       "1        0.80\n",
       "2        0.11\n",
       "3        0.72\n",
       "4        0.37\n",
       "         ... \n",
       "14995    0.37\n",
       "14996    0.37\n",
       "14997    0.11\n",
       "14998    0.37\n",
       "15001    0.70\n",
       "Name: satisfaction_level, Length: 15000, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 丢弃异常值\n",
    "sl_s = sl_s.dropna()\n",
    "sl_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6128393333333333"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看均值\n",
    "sl_s.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.24862338135944925"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准差\n",
    "sl_s.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最大值\n",
    "sl_s.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.09"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最小值\n",
    "sl_s.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 中位数\n",
    "sl_s.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.44"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下四分位数\n",
    "sl_s.quantile(q=0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.82"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上四分位数\n",
    "sl_s.quantile(q=0.75)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.47643761717258093"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 偏态系数-偏度\n",
    "# 负偏，均值偏小，大部分数比均值大\n",
    "sl_s.skew()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.6706959323886252"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 峰态系数-峰度\n",
    "sl_s.kurt()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "直方图分布分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 195, 1214,  532,  974, 1668, 2146, 1973, 2074, 2220, 2004],\n",
       "       dtype=int64),\n",
       " array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取这个指标离散化的分布\n",
    "# 获取分布的数字,bins指切分大小\n",
    "# 直方图数字查看，每两个间隔有多少值\n",
    "np.histogram(sl_s.values,bins=np.arange(0.0,1.1,0.1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第二列：过去评估last_evaluation分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0             0.53\n",
       "1             0.86\n",
       "2             0.88\n",
       "3             0.87\n",
       "4             0.52\n",
       "           ...    \n",
       "14997         0.96\n",
       "14998         0.52\n",
       "14999         0.52\n",
       "15000    999999.00\n",
       "15001         0.40\n",
       "Name: last_evaluation, Length: 15002, dtype: float64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s = df['last_evaluation']\n",
    "le_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: last_evaluation, dtype: float64)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s[le_s.isnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "67.37373216904412"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8164.407523745649"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.72"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "999999.0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.36"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "122.48265175204614"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.skew()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15001.999986807796"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.kurt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15000    999999.0\n",
       "Name: last_evaluation, dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s[le_s>1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "筛选掉在计算范围外的异常值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        0.53\n",
       "1        0.86\n",
       "2        0.88\n",
       "3        0.87\n",
       "4        0.52\n",
       "         ... \n",
       "14996    0.53\n",
       "14997    0.96\n",
       "14998    0.52\n",
       "14999    0.52\n",
       "15001    0.40\n",
       "Name: last_evaluation, Length: 15001, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q_low = le_s.quantile(q=0.25)\n",
    "q_high = le_s.quantile(q=0.75)\n",
    "# 四分位间距\n",
    "q_interval = q_high - q_low\n",
    "# k为系数\n",
    "k = 1.5\n",
    "# 筛选异常值\n",
    "le_s = le_s[le_s<q_high+k*q_interval][le_s>q_low-k*q_interval]\n",
    "le_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([   0,    0,    0,  179, 1390, 3396, 2234, 2062, 2752, 2988],\n",
       "       dtype=int64),\n",
       " array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]))"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.histogram(le_s.values,bins=np.arange(0.0,1.1,0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7160675954936337"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.17118464250786233"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.72"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.36"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.02653253746872579"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.skew()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.2390454655108427"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le_s.kurt()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第三列：工程数量number_project分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        2\n",
       "1        5\n",
       "2        7\n",
       "3        5\n",
       "4        2\n",
       "        ..\n",
       "14997    6\n",
       "14998    2\n",
       "14999    2\n",
       "15000    2\n",
       "15001    2\n",
       "Name: number_project, Length: 15002, dtype: int64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s = df['number_project']\n",
    "np_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: number_project, dtype: int64)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s[np_s.isnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.8026929742700974"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.232732779200601"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3377744235231047"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s.skew()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.49580962709450604"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s.kurt()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结构分析--静态结构分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4    4365\n",
       "3    4055\n",
       "5    2761\n",
       "2    2391\n",
       "6    1174\n",
       "7     256\n",
       "Name: number_project, dtype: int64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算每个数字出现多少次\n",
    "np_s.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4    0.290961\n",
       "3    0.270297\n",
       "5    0.184042\n",
       "2    0.159379\n",
       "6    0.078256\n",
       "7    0.017064\n",
       "Name: number_project, dtype: float64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算构成和每个数的比例\n",
    "np_s.value_counts(normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2    0.159379\n",
       "3    0.270297\n",
       "4    0.290961\n",
       "5    0.184042\n",
       "6    0.078256\n",
       "7    0.017064\n",
       "Name: number_project, dtype: float64"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_s.value_counts(normalize=True).sort_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第四列：平均每月工作时间average_monthly_hours分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        157\n",
       "1        262\n",
       "2        272\n",
       "3        223\n",
       "4        159\n",
       "        ... \n",
       "14997    280\n",
       "14998    158\n",
       "14999    158\n",
       "15000    158\n",
       "15001    158\n",
       "Name: average_monthly_hours, Length: 15002, dtype: int64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amh_s = df['average_monthly_hours']\n",
    "amh_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "201.0417277696307"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amh_s.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49.94181527437925"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amh_s.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200.0"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amh_s.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "310"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amh_s.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "96"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amh_s.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.05322458779916304"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amh_s.skew()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.1350158577565719"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amh_s.kurt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        157\n",
       "1        262\n",
       "2        272\n",
       "3        223\n",
       "4        159\n",
       "        ... \n",
       "14997    280\n",
       "14998    158\n",
       "14999    158\n",
       "15000    158\n",
       "15001    158\n",
       "Name: average_monthly_hours, Length: 15002, dtype: int64"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q_low = amh_s.quantile(q=0.25)\n",
    "q_high = amh_s.quantile(q=0.75)\n",
    "# 四分位间距\n",
    "q_interval = q_high - q_low\n",
    "# k为系数\n",
    "k = 1.5\n",
    "# 筛选异常值\n",
    "amh_s = amh_s[amh_s<q_high+k*q_interval][amh_s>q_low-k*q_interval]\n",
    "amh_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 367, 1240, 2736, 1722, 1628, 1712, 1906, 2240, 1127,  324],\n",
       "       dtype=int64),\n",
       " array([ 96. , 117.4, 138.8, 160.2, 181.6, 203. , 224.4, 245.8, 267.2,\n",
       "        288.6, 310. ]))"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 连续值的直方图分布分析，分成10份\n",
    "np.histogram(amh_s.values,bins=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 168,  171,  147,  807, 1153, 1234, 1075,  824,  818,  758,  751,\n",
       "         738,  856,  824,  987, 1002, 1045,  935,  299,  193,  131,   86],\n",
       "       dtype=int64),\n",
       " array([ 96, 106, 116, 126, 136, 146, 156, 166, 176, 186, 196, 206, 216,\n",
       "        226, 236, 246, 256, 266, 276, 286, 296, 306, 316], dtype=int64))"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 左闭右开\n",
    "np.histogram(amh_s.values,bins=np.arange(amh_s.min(),amh_s.max()+10,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(146.0, 156.0]     1277\n",
       "(136.0, 146.0]     1159\n",
       "(256.0, 266.0]     1063\n",
       "(236.0, 246.0]     1006\n",
       "(156.0, 166.0]      995\n",
       "(246.0, 256.0]      987\n",
       "(126.0, 136.0]      886\n",
       "(216.0, 226.0]      873\n",
       "(266.0, 276.0]      860\n",
       "(166.0, 176.0]      832\n",
       "(226.0, 236.0]      814\n",
       "(176.0, 186.0]      813\n",
       "(186.0, 196.0]      761\n",
       "(196.0, 206.0]      755\n",
       "(206.0, 216.0]      731\n",
       "(276.0, 286.0]      319\n",
       "(95.999, 106.0]     187\n",
       "(286.0, 296.0]      164\n",
       "(116.0, 126.0]      162\n",
       "(106.0, 116.0]      162\n",
       "(296.0, 306.0]      128\n",
       "(306.0, 316.0]       68\n",
       "Name: average_monthly_hours, dtype: int64"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算每个值数量\n",
    "# 左开右闭\n",
    "amh_s.value_counts(bins=np.arange(amh_s.min(),amh_s.max()+10,10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第五列：在公司的时间time_spend_company分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        3\n",
       "1        6\n",
       "2        4\n",
       "3        5\n",
       "4        3\n",
       "        ..\n",
       "14997    4\n",
       "14998    3\n",
       "14999    3\n",
       "15000    3\n",
       "15001    2\n",
       "Name: time_spend_company, Length: 15002, dtype: int64"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsc_s = df['time_spend_company']\n",
    "tsc_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2     3245\n",
       "3     6445\n",
       "4     2557\n",
       "5     1473\n",
       "6      718\n",
       "7      188\n",
       "8      162\n",
       "10     214\n",
       "Name: time_spend_company, dtype: int64"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsc_s.value_counts().sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.498066924410079"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsc_s.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第六列：工作事故Work_accident分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        0\n",
       "1        0\n",
       "2        0\n",
       "3        0\n",
       "4        0\n",
       "        ..\n",
       "14997    0\n",
       "14998    0\n",
       "14999    0\n",
       "15000    0\n",
       "15001    0\n",
       "Name: Work_accident, Length: 15002, dtype: int64"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wa_s = df['Work_accident']\n",
    "wa_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    12833\n",
       "1     2169\n",
       "Name: Work_accident, dtype: int64"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wa_s.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.14458072257032395"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 事故率，在0-1的取值，事故率和均值是相等的\n",
    "wa_s.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第七列：最近离职left分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        1\n",
       "1        1\n",
       "2        1\n",
       "3        1\n",
       "4        1\n",
       "        ..\n",
       "14997    1\n",
       "14998    1\n",
       "14999    1\n",
       "15000    1\n",
       "15001    1\n",
       "Name: left, Length: 15002, dtype: int64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l_s = df['left']\n",
    "l_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    11428\n",
       "1     3574\n",
       "Name: left, dtype: int64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l_s.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第八列：过去5年的提升promotion_last_5years分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        0\n",
       "1        0\n",
       "2        0\n",
       "3        0\n",
       "4        0\n",
       "        ..\n",
       "14997    0\n",
       "14998    0\n",
       "14999    0\n",
       "15000    0\n",
       "15001    0\n",
       "Name: promotion_last_5years, Length: 15002, dtype: int64"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pl5_s = df['promotion_last_5years']\n",
    "pl5_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    14683\n",
       "1      319\n",
       "Name: promotion_last_5years, dtype: int64"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pl5_s.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第十列：工资salary分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0           low\n",
       "1        medium\n",
       "2        medium\n",
       "3           low\n",
       "4           low\n",
       "          ...  \n",
       "14997       low\n",
       "14998       low\n",
       "14999       low\n",
       "15000       low\n",
       "15001       nme\n",
       "Name: salary, Length: 15002, dtype: object"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_s = df['salary']\n",
    "s_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "low       7318\n",
       "medium    6446\n",
       "high      1237\n",
       "nme          1\n",
       "Name: salary, dtype: int64"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_s.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0           low\n",
       "1        medium\n",
       "2        medium\n",
       "3           low\n",
       "4           low\n",
       "          ...  \n",
       "14996       low\n",
       "14997       low\n",
       "14998       low\n",
       "14999       low\n",
       "15000       low\n",
       "Name: salary, Length: 15001, dtype: object"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_s = s_s.where(s_s!='nme').dropna()\n",
    "s_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "low       7318\n",
       "medium    6446\n",
       "high      1237\n",
       "Name: salary, dtype: int64"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_s.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第九列：部门department分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0          sales\n",
       "1          sales\n",
       "2          sales\n",
       "3          sales\n",
       "4          sales\n",
       "          ...   \n",
       "14997    support\n",
       "14998    support\n",
       "14999    support\n",
       "15000       sale\n",
       "15001       sale\n",
       "Name: department, Length: 15002, dtype: object"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_s = df['department']\n",
    "d_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sales          0.275963\n",
       "technical      0.181309\n",
       "support        0.148647\n",
       "IT             0.081789\n",
       "product_mng    0.060125\n",
       "marketing      0.057192\n",
       "RandD          0.052460\n",
       "accounting     0.051127\n",
       "hr             0.049260\n",
       "management     0.041994\n",
       "sale           0.000133\n",
       "Name: department, dtype: float64"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_s.value_counts(normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0          sales\n",
       "1          sales\n",
       "2          sales\n",
       "3          sales\n",
       "4          sales\n",
       "          ...   \n",
       "14997    support\n",
       "14998    support\n",
       "14999    support\n",
       "15000        NaN\n",
       "15001        NaN\n",
       "Name: department, Length: 15002, dtype: object"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_s = d_s.where(d_s!='sale')\n",
    "d_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0          sales\n",
       "1          sales\n",
       "2          sales\n",
       "3          sales\n",
       "4          sales\n",
       "          ...   \n",
       "14995    support\n",
       "14996    support\n",
       "14997    support\n",
       "14998    support\n",
       "14999    support\n",
       "Name: department, Length: 15000, dtype: object"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_s = d_s.dropna()\n",
    "d_s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 简单对比分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>satisfaction_level</th>\n",
       "      <th>last_evaluation</th>\n",
       "      <th>number_project</th>\n",
       "      <th>average_monthly_hours</th>\n",
       "      <th>time_spend_company</th>\n",
       "      <th>Work_accident</th>\n",
       "      <th>left</th>\n",
       "      <th>promotion_last_5years</th>\n",
       "      <th>department</th>\n",
       "      <th>salary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>0.38</td>\n",
       "      <td>0.53</td>\n",
       "      <td>2</td>\n",
       "      <td>157</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>0.80</td>\n",
       "      <td>0.86</td>\n",
       "      <td>5</td>\n",
       "      <td>262</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.88</td>\n",
       "      <td>7</td>\n",
       "      <td>272</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0.72</td>\n",
       "      <td>0.87</td>\n",
       "      <td>5</td>\n",
       "      <td>223</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>159</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14995</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.48</td>\n",
       "      <td>2</td>\n",
       "      <td>160</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14996</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.53</td>\n",
       "      <td>2</td>\n",
       "      <td>143</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14997</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.96</td>\n",
       "      <td>6</td>\n",
       "      <td>280</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14998</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>158</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15001</td>\n",
       "      <td>0.70</td>\n",
       "      <td>0.40</td>\n",
       "      <td>2</td>\n",
       "      <td>158</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sale</td>\n",
       "      <td>nme</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>15000 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       satisfaction_level  last_evaluation  number_project  \\\n",
       "0                    0.38             0.53               2   \n",
       "1                    0.80             0.86               5   \n",
       "2                    0.11             0.88               7   \n",
       "3                    0.72             0.87               5   \n",
       "4                    0.37             0.52               2   \n",
       "...                   ...              ...             ...   \n",
       "14995                0.37             0.48               2   \n",
       "14996                0.37             0.53               2   \n",
       "14997                0.11             0.96               6   \n",
       "14998                0.37             0.52               2   \n",
       "15001                0.70             0.40               2   \n",
       "\n",
       "       average_monthly_hours  time_spend_company  Work_accident  left  \\\n",
       "0                        157                   3              0     1   \n",
       "1                        262                   6              0     1   \n",
       "2                        272                   4              0     1   \n",
       "3                        223                   5              0     1   \n",
       "4                        159                   3              0     1   \n",
       "...                      ...                 ...            ...   ...   \n",
       "14995                    160                   3              0     1   \n",
       "14996                    143                   3              0     1   \n",
       "14997                    280                   4              0     1   \n",
       "14998                    158                   3              0     1   \n",
       "15001                    158                   2              0     1   \n",
       "\n",
       "       promotion_last_5years department  salary  \n",
       "0                          0      sales     low  \n",
       "1                          0      sales  medium  \n",
       "2                          0      sales  medium  \n",
       "3                          0      sales     low  \n",
       "4                          0      sales     low  \n",
       "...                      ...        ...     ...  \n",
       "14995                      0    support     low  \n",
       "14996                      0    support     low  \n",
       "14997                      0    support     low  \n",
       "14998                      0    support     low  \n",
       "15001                      0       sale     nme  \n",
       "\n",
       "[15000 rows x 10 columns]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先剔除异常值\n",
    "df = df.dropna(axis=0,how='any')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>satisfaction_level</th>\n",
       "      <th>last_evaluation</th>\n",
       "      <th>number_project</th>\n",
       "      <th>average_monthly_hours</th>\n",
       "      <th>time_spend_company</th>\n",
       "      <th>Work_accident</th>\n",
       "      <th>left</th>\n",
       "      <th>promotion_last_5years</th>\n",
       "      <th>department</th>\n",
       "      <th>salary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>0.38</td>\n",
       "      <td>0.53</td>\n",
       "      <td>2</td>\n",
       "      <td>157</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>0.80</td>\n",
       "      <td>0.86</td>\n",
       "      <td>5</td>\n",
       "      <td>262</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.88</td>\n",
       "      <td>7</td>\n",
       "      <td>272</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0.72</td>\n",
       "      <td>0.87</td>\n",
       "      <td>5</td>\n",
       "      <td>223</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>159</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>sales</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14994</td>\n",
       "      <td>0.40</td>\n",
       "      <td>0.57</td>\n",
       "      <td>2</td>\n",
       "      <td>151</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14995</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.48</td>\n",
       "      <td>2</td>\n",
       "      <td>160</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14996</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.53</td>\n",
       "      <td>2</td>\n",
       "      <td>143</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14997</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.96</td>\n",
       "      <td>6</td>\n",
       "      <td>280</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14998</td>\n",
       "      <td>0.37</td>\n",
       "      <td>0.52</td>\n",
       "      <td>2</td>\n",
       "      <td>158</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>support</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>14999 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       satisfaction_level  last_evaluation  number_project  \\\n",
       "0                    0.38             0.53               2   \n",
       "1                    0.80             0.86               5   \n",
       "2                    0.11             0.88               7   \n",
       "3                    0.72             0.87               5   \n",
       "4                    0.37             0.52               2   \n",
       "...                   ...              ...             ...   \n",
       "14994                0.40             0.57               2   \n",
       "14995                0.37             0.48               2   \n",
       "14996                0.37             0.53               2   \n",
       "14997                0.11             0.96               6   \n",
       "14998                0.37             0.52               2   \n",
       "\n",
       "       average_monthly_hours  time_spend_company  Work_accident  left  \\\n",
       "0                        157                   3              0     1   \n",
       "1                        262                   6              0     1   \n",
       "2                        272                   4              0     1   \n",
       "3                        223                   5              0     1   \n",
       "4                        159                   3              0     1   \n",
       "...                      ...                 ...            ...   ...   \n",
       "14994                    151                   3              0     1   \n",
       "14995                    160                   3              0     1   \n",
       "14996                    143                   3              0     1   \n",
       "14997                    280                   4              0     1   \n",
       "14998                    158                   3              0     1   \n",
       "\n",
       "       promotion_last_5years department  salary  \n",
       "0                          0      sales     low  \n",
       "1                          0      sales  medium  \n",
       "2                          0      sales  medium  \n",
       "3                          0      sales     low  \n",
       "4                          0      sales     low  \n",
       "...                      ...        ...     ...  \n",
       "14994                      0    support     low  \n",
       "14995                      0    support     low  \n",
       "14996                      0    support     low  \n",
       "14997                      0    support     low  \n",
       "14998                      0    support     low  \n",
       "\n",
       "[14999 rows x 10 columns]"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df[df['salary']!='nme']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>satisfaction_level</th>\n",
       "      <th>last_evaluation</th>\n",
       "      <th>number_project</th>\n",
       "      <th>average_monthly_hours</th>\n",
       "      <th>time_spend_company</th>\n",
       "      <th>Work_accident</th>\n",
       "      <th>left</th>\n",
       "      <th>promotion_last_5years</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>department</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>IT</td>\n",
       "      <td>0.618142</td>\n",
       "      <td>0.716830</td>\n",
       "      <td>3.816626</td>\n",
       "      <td>202.215974</td>\n",
       "      <td>3.468623</td>\n",
       "      <td>0.133659</td>\n",
       "      <td>0.222494</td>\n",
       "      <td>0.002445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>RandD</td>\n",
       "      <td>0.619822</td>\n",
       "      <td>0.712122</td>\n",
       "      <td>3.853875</td>\n",
       "      <td>200.800508</td>\n",
       "      <td>3.367217</td>\n",
       "      <td>0.170267</td>\n",
       "      <td>0.153748</td>\n",
       "      <td>0.034307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>accounting</td>\n",
       "      <td>0.582151</td>\n",
       "      <td>0.717718</td>\n",
       "      <td>3.825293</td>\n",
       "      <td>201.162973</td>\n",
       "      <td>3.522816</td>\n",
       "      <td>0.125163</td>\n",
       "      <td>0.265971</td>\n",
       "      <td>0.018253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>hr</td>\n",
       "      <td>0.598809</td>\n",
       "      <td>0.708850</td>\n",
       "      <td>3.654939</td>\n",
       "      <td>198.684709</td>\n",
       "      <td>3.355886</td>\n",
       "      <td>0.120433</td>\n",
       "      <td>0.290934</td>\n",
       "      <td>0.020298</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>management</td>\n",
       "      <td>0.621349</td>\n",
       "      <td>0.724000</td>\n",
       "      <td>3.860317</td>\n",
       "      <td>201.249206</td>\n",
       "      <td>4.303175</td>\n",
       "      <td>0.163492</td>\n",
       "      <td>0.144444</td>\n",
       "      <td>0.109524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>marketing</td>\n",
       "      <td>0.618601</td>\n",
       "      <td>0.715886</td>\n",
       "      <td>3.687646</td>\n",
       "      <td>199.385781</td>\n",
       "      <td>3.569930</td>\n",
       "      <td>0.160839</td>\n",
       "      <td>0.236597</td>\n",
       "      <td>0.050117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>product_mng</td>\n",
       "      <td>0.619634</td>\n",
       "      <td>0.714756</td>\n",
       "      <td>3.807095</td>\n",
       "      <td>199.965632</td>\n",
       "      <td>3.475610</td>\n",
       "      <td>0.146341</td>\n",
       "      <td>0.219512</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>sales</td>\n",
       "      <td>0.614447</td>\n",
       "      <td>0.709717</td>\n",
       "      <td>3.776329</td>\n",
       "      <td>200.911353</td>\n",
       "      <td>3.534058</td>\n",
       "      <td>0.141787</td>\n",
       "      <td>0.244928</td>\n",
       "      <td>0.024155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>support</td>\n",
       "      <td>0.618300</td>\n",
       "      <td>0.723109</td>\n",
       "      <td>3.803948</td>\n",
       "      <td>200.758188</td>\n",
       "      <td>3.393001</td>\n",
       "      <td>0.154778</td>\n",
       "      <td>0.248991</td>\n",
       "      <td>0.008973</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>technical</td>\n",
       "      <td>0.607897</td>\n",
       "      <td>0.721099</td>\n",
       "      <td>3.877941</td>\n",
       "      <td>202.497426</td>\n",
       "      <td>3.411397</td>\n",
       "      <td>0.140074</td>\n",
       "      <td>0.256250</td>\n",
       "      <td>0.010294</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             satisfaction_level  last_evaluation  number_project  \\\n",
       "department                                                         \n",
       "IT                     0.618142         0.716830        3.816626   \n",
       "RandD                  0.619822         0.712122        3.853875   \n",
       "accounting             0.582151         0.717718        3.825293   \n",
       "hr                     0.598809         0.708850        3.654939   \n",
       "management             0.621349         0.724000        3.860317   \n",
       "marketing              0.618601         0.715886        3.687646   \n",
       "product_mng            0.619634         0.714756        3.807095   \n",
       "sales                  0.614447         0.709717        3.776329   \n",
       "support                0.618300         0.723109        3.803948   \n",
       "technical              0.607897         0.721099        3.877941   \n",
       "\n",
       "             average_monthly_hours  time_spend_company  Work_accident  \\\n",
       "department                                                              \n",
       "IT                      202.215974            3.468623       0.133659   \n",
       "RandD                   200.800508            3.367217       0.170267   \n",
       "accounting              201.162973            3.522816       0.125163   \n",
       "hr                      198.684709            3.355886       0.120433   \n",
       "management              201.249206            4.303175       0.163492   \n",
       "marketing               199.385781            3.569930       0.160839   \n",
       "product_mng             199.965632            3.475610       0.146341   \n",
       "sales                   200.911353            3.534058       0.141787   \n",
       "support                 200.758188            3.393001       0.154778   \n",
       "technical               202.497426            3.411397       0.140074   \n",
       "\n",
       "                 left  promotion_last_5years  \n",
       "department                                    \n",
       "IT           0.222494               0.002445  \n",
       "RandD        0.153748               0.034307  \n",
       "accounting   0.265971               0.018253  \n",
       "hr           0.290934               0.020298  \n",
       "management   0.144444               0.109524  \n",
       "marketing    0.236597               0.050117  \n",
       "product_mng  0.219512               0.000000  \n",
       "sales        0.244928               0.024155  \n",
       "support      0.248991               0.008973  \n",
       "technical    0.256250               0.010294  "
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对部门进行对比，先分组，再聚合\n",
    "df.groupby('department').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>last_evaluation</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>department</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>IT</td>\n",
       "      <td>0.716830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>RandD</td>\n",
       "      <td>0.712122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>accounting</td>\n",
       "      <td>0.717718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>hr</td>\n",
       "      <td>0.708850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>management</td>\n",
       "      <td>0.724000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>marketing</td>\n",
       "      <td>0.715886</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>product_mng</td>\n",
       "      <td>0.714756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>sales</td>\n",
       "      <td>0.709717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>support</td>\n",
       "      <td>0.723109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>technical</td>\n",
       "      <td>0.721099</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             last_evaluation\n",
       "department                  \n",
       "IT                  0.716830\n",
       "RandD               0.712122\n",
       "accounting          0.717718\n",
       "hr                  0.708850\n",
       "management          0.724000\n",
       "marketing           0.715886\n",
       "product_mng         0.714756\n",
       "sales               0.709717\n",
       "support             0.723109\n",
       "technical           0.721099"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对表中某几个字段进行对比\n",
    "df.loc[:,['last_evaluation','department']].groupby('department').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "department\n",
       "IT             212\n",
       "RandD          210\n",
       "accounting     213\n",
       "hr             212\n",
       "management     210\n",
       "marketing      214\n",
       "product_mng    212\n",
       "sales          214\n",
       "support        214\n",
       "technical      213\n",
       "Name: average_monthly_hours, dtype: int64"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用自己定义的函数进行对比，这里计算极差：最大值-最小值\n",
    "df.loc[:,['average_monthly_hours','department']].groupby('department')['average_monthly_hours'].apply(lambda x:x.max()-x.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
